package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.PictureUseTypeEnum;
import com.jimmy.medical.model.SignInfo;
import com.jimmy.medical.service.ICheckFlowOrderService;
import com.jimmy.medical.service.ISignFlowOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Qualifier("checkFlowOrderService")
public class CheckFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements ICheckFlowOrderService {

    @Override
    public Long saveAffirmInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "确认接单", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveAffirmDial(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "致电客户", 2);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveCheckItem(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "开检查单", 4);

        List<UploadFile> fileList = uploadFileRepository.findAllByRelatedKeyAndUseType(flowOrder.getOrder().getId().toString(), PictureUseTypeEnum.CHECK.getKey());
        uploadFileRepository.delete(fileList);

        if(StringUtils.isNotBlank(photoIds)) {
            String[] idArray = photoIds.split(",");
            for (String photoId : idArray) {
                UploadFile photo = uploadFileRepository.findOne(Long.valueOf(photoId));
                photo.setRelatedKey(flowOrder.getOrder().getId().toString());
                photo.setUseType(PictureUseTypeEnum.CHECK.getKey());
                uploadFileRepository.save(photo);
            }


        }

        return getCountDown(flowOrder);
    }

    @Override
    public List<UploadFile> getCheckItem(Long flowOrderId) {
        FlowOrder flowOrder = repository.findOne(flowOrderId);
        Order order = flowOrder.getOrder();

        List<UploadFile> fileList = uploadFileRepository.findAllByRelatedKeyAndUseType(order.getId().toString(), PictureUseTypeEnum.CHECK.getKey());

        return fileList;
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话告知", 5);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getOrderId(), message.getUserId(), "短信告知", 6);
        return getCountDown(flowOrder);
    }

    @Override
    public FlowOrder submitCheck(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成挂号", 7);

        //生成下一个工单,转入中台指派
        return getNextFlowOrder(flowOrder);
    }

    @Override
    public Long saveReportAffirmInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "确认接单", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public List<UploadFile> getTakeReport(Long flowOrderId) {
        FlowOrder flowOrder = repository.findOne(flowOrderId);
        Order order = flowOrder.getOrder();

        List<UploadFile> fileList = uploadFileRepository.findAllByRelatedKeyAndUseType(order.getId().toString(), PictureUseTypeEnum.CHECK_REPORT.getKey());

        return fileList;
    }

    @Override
    public Long saveTakeReport(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "取报告", 2);

        List<UploadFile> fileList = uploadFileRepository.findAllByRelatedKeyAndUseType(flowOrder.getOrder().getId().toString(), PictureUseTypeEnum.CHECK_REPORT.getKey());
        uploadFileRepository.delete(fileList);

        if(StringUtils.isNotBlank(photoIds)) {
            String[] idArray = photoIds.split(",");
            for (String photoId : idArray) {
                UploadFile photo = uploadFileRepository.findOne(Long.valueOf(photoId));
                photo.setRelatedKey(flowOrder.getOrder().getId().toString());
                photo.setUseType(PictureUseTypeEnum.CHECK_REPORT.getKey());
                uploadFileRepository.save(photo);
            }
        }

        return getCountDown(flowOrder);
    }

    @Override
    public Long saveReportNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话告知", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveReportMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getOrderId(), message.getUserId(), "短信告知", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public FlowOrder submitCheckReport(Long flowOrderId, Long userId) {
        process(flowOrderId, userId, "完成取报告", 5);
        return null;
    }
}